package com.liurong.quartz.util.properties;

import java.io.IOException;
import java.util.Properties;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesFactoryBean implements FactoryBean<Properties>, InitializingBean {
	private static Properties properties = new Properties();
	private Resource[] locations;

	public PropertiesFactoryBean(Resource[] locations) {
		this.locations = locations;
	}

	private void createProperties() {
		if (locations != null && locations.length > 0) {
			for (Resource location : locations) {
				try {
					PropertiesLoaderUtils.fillProperties(properties, location);
				} catch (IOException e) {
					System.out.println("加载配置出了异常！" + e);
				}
			}
		} else {
			System.out.println("需要有配置中心！");
		}
	}

	public static Properties getProperties() {
		return PropertiesFactoryBean.properties;
	}

	public Properties getObject() throws Exception {
		return PropertiesFactoryBean.properties;
	}

	public Class<?> getObjectType() {
		return Properties.class;
	}

	public boolean isSingleton() {
		return true;
	}

	public void afterPropertiesSet() throws Exception {
		this.createProperties();
	}
}
